package com.wavehi.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Collection;
import java.util.UUID;

/**
 * @Author WaveHi
 * @Date 22/9/2021 下午 3:11
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@WebServlet("/userServlet")
@MultipartConfig
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //统一字符集
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("UTF-8");
        //获取参数值
        String username = req.getParameter("username");
        System.out.println("username : " + username);
        //读取文件
        Part userImage = req.getPart("userImage");
        //获取头信息
        //获取头信息的多个值
        System.out.println("content-disposition : " + userImage.getHeader("content-disposition"));
        System.out.println("content-type : " + userImage.getHeader("content-type"));
        System.out.println("ContentType" + userImage.getContentType());
        //获取头信息键组成的集合
        System.out.println("Headers : " + userImage.getHeaderNames());
        //获取文件名
        System.out.println("Name : " + userImage.getName());
        System.out.println("SubmittedFileName : " + userImage.getSubmittedFileName());
        Collection<Part> parts = req.getParts();
        System.out.println(parts);
//        String realPath = getServletContext().getRealPath("/static/image/register") + "/" + UUID.randomUUID() + userImage.getSubmittedFileName().substring(userImage.getSubmittedFileName().lastIndexOf("."));
//        System.out.println(realPath);
//        userImage.write("E:/idea/wolf_javaweb/day10_file/web/static/image/register/" + "1" + userImage.getSubmittedFileName().substring(userImage.getSubmittedFileName().lastIndexOf(".")));
//        userImage.write(realPath);
    }
}
